#!/usr/bin/python
#coding:gbk

import sys
import os
class Generator :
    path = "../list";
    template = {};
    target = {};
    def __init__(self):
        print 'init...'
        #if os.access("data", os.F_OK):
        #    os.removedirs("data");
        os.system("rm -rf data");
        os.mkdir("data");
        print 'end init...'
        self.template['SortedTitleListTemplate'] = self.path + "/sorted_title.tpl";
        self.template['SortedTitleList'] = self.path + "/sorted_title_list.txt";
        #self.target['SortedList'] = self.path + "/sortedA.dat"
        self.target['SortedList'] = "xxxx";

    def genSortedListRawData(self):
        f = open("song.txt");
        song = f.readline().strip(); #32
        singer = f.readline().strip(); #24
        id = f.readline().strip(); #18
        price = f.readline().strip(); #8
        date = f.readline().strip(); #12
        f.close();
        print song
        for i in range(1, 11):
            file = "data/" + str(i) + ".txt";
            f = open(file, "w+");
            for ii in range(1, 11):

                song1 = song + str(i) + str(ii) + '\n';
                f.write(song1);
            f.close();
    def genSortedList(self):
        count = 0;
        content = "";

        f = open("song.txt");
        song = f.readline().strip(); #32
        singer = f.readline().strip(); #24
        id = f.readline().strip(); #18
        price = f.readline().strip(); #8
        date = f.readline().strip(); #12
        f.close();
        for i in range(1, 11):
            file = "data/" + str(i) + ".txt";
            f = open(file, "r");
            for line in f:
                print line.strip();
                content = content + id.ljust(18, '\0');
                content = content + line.strip().ljust(32, '\0');
                content = content + singer.ljust(24, '\0');
                content = content + price.ljust(8, '\0');
                content = content + date.ljust(12, '\0');
                count = count + 1;
            f.close();
        count = "%04d" % count;
        content = count + content;
        f = open(self.target['SortedList'], 'w+');
        f.write(content);
        f.close();
    def genSortedTitleList(self):
        tpl = open(self.template['SortedTitleListTemplate']);
        res = "";
        count = 0;
        for i in tpl:
            content = i.strip().ljust(20,'\0');
            print "content = %s, len = %d" % (content, len(content));
            res = res + content;
            count = count + 1;
        tpl.close();
        count = "%04d" % count;
        res = count + res;
        target = open(self.template['SortedTitleList'], "w+");
        target.write(res);
        target.close();
        print count;

if __name__ == '__main__':
    generator = Generator();
    #tm.orderTone();
    generator.genSortedTitleList();
    generator.genSortedListRawData();
    generator.genSortedList();
    #tm.queryDefaultTone();
    #tm.setDefaultTone();
    #tm.queryMyTone();
    #tm.delTone();
    #for v in tm.url.values():
        #    print v;



